<?php
/**
 * 
 * @author tangbl
 * 优惠劵实体类
 */
class YhjAction extends CommonAction{
	
	/**
	 * 发放优惠券
	 */
	public function yhj_add() {
		$actyhj  = M("Actyhj");
		if(IS_POST){
			$map['yhjid']    = $this->_param("yhjid");
			$map['mobile']   = $this->_param("mobile");
			$map['realname'] = $this->_param("realname");
			$json = sendRest("Coupon/get", $map);
			
			if($json['status']==1){
				$this->assign("jumpUrl",U('/Yhj/yhj_add'));
				$this->success("优惠券发放成功~！");
			}else{
				$this->error($json['info']);
			}
		}else{
			$map['fields'] = "id,title";
			$json = sendRest("Coupon/uselist",$map);
			
			$this->assign("yhjlist",$json);
			$this->display();		
		}
	}
	
	/**
	 * 优惠券的使用
	 */
	public function yhj_use(){
		if(IS_POST){//处理优惠券使用
			
			$coupon = $this->_param("coupon");
			$remark = $this->_param("remark");
			if(empty($coupon))$this->error("消费金额不能为空");
			if(empty($remark))$this->error("请输入消费备注");
			
			$map['couponid'] = $this->_param("yhjid");      //优惠券领取id
			$map['total']    = $this->_param("total");      //消费金额
			$map['coupon']   = $this->_param("coupon");     //应付金额
			$map['remark']   = $this->_param("remark");     //消费金额
			$map['addid']    = session(C('USER_AUTH_KEY')); //操作者
			$map['userid']   = $this->_param("userid");     //用户编号
			$map['type']     = 1;                           //操作类型(1线下 2线上[默认])
			$json = sendRest("Coupon/addlog",$map);
			
			if($json['status']==1){
				$this->assign("jumpUrl",U('Yhj/yhj_ship'));
				$this->success(L('yhj_form_ele_usesuccess1'));
			}else{
				$this->error($json['info']);
			}
			
		}elseif(IS_AJAX){//根据优惠券id以及金额，异步获取优惠金额
			
			$map['couponid'] = $this->_param("id");
			$map['total']    = $this->_param("total");
			$json = sendRest("Coupon/find",$map);

			$this->ajaxReturn($json['data']);
			
		}else{//显示优惠券使用页面
			
			$id = $this->_param("id");
			
			$map['id']     = $id;
			$map['format'] = 1;
			$json = sendRest("Coupon/view", $map);
			
			$this->assign("detail",$json);			
			$this->display();	
		}	
	}
	
	/**
	 * 优惠券匹配电话号码
	 */
	public function yhj_ship(){
		if(IS_POST){
			$mobile = $this->_param("mobile");
			if(empty($mobile)){
				$this->error(L('cfg_form_tipmsg_illgel'));	
			}else{
				$map['mobile'] = $mobile;
				$json = sendRest("Coupon/select",$map);
				
				if($json['status']==1){
					$this->assign("jumpUrl",U('Yhj/yhj_uselist','mobile='.$mobile));
					$this->success(L('yhj_form_ele_successmatch'));
				}else{
					$this->error(L('yhj_form_ele_failmatch'));
				}
			}
		}else{
			$this->display();
		}	
	}
	
	/**
	 * 用户优惠券列表
	 */
	public function yhj_uselist(){
		if(IS_AJAX){
			$map['couponid'] = $this->_param('id');
			$map['userid']   = $this->_param("userid");
			$json = sendRest("Coupon/del", $map);
			$this->ajaxReturn($json);
		}else{
			$map['mobile'] = $this->_param("mobile");
			$map['format'] = 1;
			$json = sendRest("Coupon/select",$map);
			
			foreach($json['data'] as $k=>$v){
				$expires = $v['expires'] > time()?$v['expires']:"已过期";
				$json['data'][$k]['is_expires'] = $expires;
			}
			
			$this->assign("time",time());
			$this->assign("list",$json);
			$this->display();
		}
	}
	
	/**
	 * 优惠券管理列表
	 */
	public function yhj_list(){
		
		$map['format'] = 1;
		$json = sendRest("Coupon/uselist",$map);
		
		$this->assign("list",$json);
		$this->display();
	}
	
	/**
	 * 优惠券领取记录
	 */
	public function yhj_records(){
		
		$map['couponid'] = $this->_param("id");
		$json = sendRest("Coupon/getlist",$map);
		
		$this->assign("list",$json);
		$this->display();
	}
	
	
	/**
	 * 优惠券统计
	 */
	public function yhj_chart(){
		$this->display();
	}
}
?>